package lumingweihua.future.cn.lumingweihua.ui.mine;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Strings;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;

/**
 * todo 修改手机号码
 * Created by z1986 on 2018/3/17.
 */

public class ModifyPhoneActivity extends BaseCompatActivity {
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.tv_getCode)
    TextView tvGetCode;
    @BindView(R.id.edit_code)
    EditText editCode;

    LoadData<String> loadData;
    LoadData<String> sendSMSData;
    CountDownTimer timer;
//    String valueCode;
    String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_modify_phone);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra(_EXTRA_String_ID);
        init();
    }

    private void init() {
        loadData = new LoadData<>(LoadData.Api.个人中心我的车辆信息修改_手机号, this);
        loadData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> httpRequest, IHttpResult<String> result) {
                showToast(result.getData());
                setResult(RESULT_OK);
                finish();
            }
        });
        sendSMSData = new LoadData<>(LoadData.Api.短信发送接口, this);
        sendSMSData._setOnLoadingListener(new SimpleHttpListener<String>() {
            @Override
            public void onLoadStart(LoadData.Api api, HttpRequest<Object> request) {
                //倒计时
                timer = new CountDownTimer(30000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        tvGetCode.setText(millisUntilFinished / 1000 + "s");
                    }

                    @Override
                    public void onFinish() {
                        timer = null;
                        tvGetCode.setText("获取验证码");
                        tvGetCode.setEnabled(true);
                    }
                };
                tvGetCode.setEnabled(false);
                timer.start();
            }

            @Override
            public void onLoadError(LoadData.Api api, HttpRequest<Object> request, IHttpResult<String> t, boolean isApiError, String errorMessage) {
                if (timer != null) {
                    timer.cancel();
                }
                tvGetCode.setText("获取验证码");
                tvGetCode.setEnabled(true);
            }

            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<String> t) {
//                valueCode = t.getData();
            }
        });
    }

    @OnClick({R.id.tv_submit, R.id.tv_getCode})
    public void onClick(View v) {
        String phone;
        switch (v.getId()) {
            case R.id.tv_submit:
                phone = editPhone.getText().toString();
                if (!_Strings.isMobilePhone(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                String code = editCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    showToast("请输入验证码");
                    return;
                }
                loadData._loadData(id, phone, code);
                break;
            case R.id.tv_getCode:
                phone = editPhone.getText().toString();
                if (!_Strings.isMobilePhone(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                sendSMSData._loadData(phone);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
